package server;
import java.util.ArrayList;


/**
 * this class represents a Reference Point.
 * Reference Points are distributed over the room and the positioning tries
 * to assign TestPoints to ReferencePoints.
 * A ReferencePoint consists 
 * - of coordinates (X,Y) (in the room as well as on the GUI)
 * - an ID that is generated from the coordinates
 * - SinglePoint which contain the actual Metric Data
 * 
 * @author nemi
 *
 */
public class ReferencePoint {
	public ArrayList<SinglePoint> singlePoints;
	public int X,Y;
	public String id;
	
	public ReferencePoint(int x, int y, String id) {
		super();
		X = x;
		Y = y;
		this.id = id;
		singlePoints=new ArrayList<SinglePoint>();
	}
	
	/**
	 * adds a SinglePoint to this ReferencePoint.
	 * Is called, when we are live and want to add a single Point.
	 * @param positioning needed to safe the ReferencePoints to the Database
	 * @return name of the new table that was created
	 */
	public String addSinglePoint(Positioning positioning){
		String newTableName="tab"+String.valueOf(X)+"_"+String.valueOf(Y)+"_"+String.valueOf(singlePoints.size());
		addSinglePoint(newTableName);
		Database.writeReferenzPointsToDB("positioning1", positioning.myRefPoints);
		return newTableName;
	}

	/**
	 * adds a SinglePoint to this ReferencePoint.
	 * Is called on startup, when we want to restore our positioning 
	 * @param tablename
	 */
	public void addSinglePoint(String tablename){
		SinglePoint newSinglePoint=new SinglePoint(tablename);
		//newSinglePoint.storeSamplesToDB();
		singlePoints.add(newSinglePoint);
	}
	
	/**
	 * is called, when we want to add samples to a ReferencePoint.
	 * They are allways added to the last SinglePoint that was added
	 * @param newSamples
	 */
	public void addSamples(ArrayList<DataSet> newSamples){
		System.out.println("adding new samples to RefPoint");
		singlePoints.get(singlePoints.size()-1).samples.addAll(newSamples);
		System.out.println("Calc the mean values");
		singlePoints.get(singlePoints.size()-1).calculateMeanValues();
		Database.writeDataSetsToDB(singlePoints.get(singlePoints.size()-1).tablename,singlePoints.get(singlePoints.size()-1).samples);		
	}
	
}
